package com.xujl.rxtask;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import com.xujl.task.follow.IFollowStep;
import com.xujl.task.follow.ITrigger;
import com.xujl.task.follow.RxFollow;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

public class FollowActivity extends Activity implements View.OnClickListener {
    private static final int FOLLOW_ONE = 1;
    private static final int FOLLOW_TWO = 2;
    private static final int FOLLOW_THREE = 3;

    @Override
    protected void onCreate (@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_follow);
        findViewById(R.id.btn1).setOnClickListener(this);

    }

    @Override
    public void onClick (View view) {
        switch (view.getId()) {
            case R.id.btn1:
                RxFollow.create()
                        .addFollow(step1)
                        .addFollow(step2)
                        .addFollow(step3)
                        .startFollow();
                break;
            default:

                break;

        }
    }

    private IFollowStep step1 = new IFollowStep() {
        @Override
        public void run (final ITrigger trigger) {
            showWindow(FOLLOW_ONE, "", new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss (DialogInterface dialogInterface) {
                    trigger.followSaveResult(FOLLOW_ONE, "result 1:哒哒哒");
                    trigger.followComplete();
                }
            });
        }
    };
    private IFollowStep step2 = new IFollowStep() {
        @Override
        public void run (final ITrigger trigger) {
            showWindow(FOLLOW_TWO, trigger.getStepResult(FOLLOW_ONE).toString(), new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss (DialogInterface dialogInterface) {
                    trigger.followSaveResult(FOLLOW_TWO, "result 2:呀呀呀");
                    trigger.followComplete();
                }
            });
        }
    };
    private IFollowStep step3 = new IFollowStep() {
        @Override
        public void run (final ITrigger trigger) {
            showWindow(FOLLOW_THREE, trigger.getStepResult(FOLLOW_ONE).toString() + "=====" + trigger.getStepResult(FOLLOW_TWO).toString(), new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss (DialogInterface dialogInterface) {
                    trigger.followComplete();
                }
            });
        }
    };

    private void showWindow (int code, String msg, DialogInterface.OnDismissListener listener) {
        new AlertDialog.Builder(this)
                .setTitle("流程" + code)
                .setMessage(msg)
                .setPositiveButton("确定", null)
                .setOnDismissListener(listener).create().show();
    }
}
